///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IMAGE_CCHANNELCONVERSION_H_
#define _BUOLA_IMAGE_CCHANNELCONVERSION_H_

#include <buola/image.h>

#define DECLARE_CHANNEL_TYPE(name,type,wtype,min,max)                                                               \
    struct TChannel_##name                                                                                          \
    {                                                                                                               \
        typedef type TType;                                                                                         \
        typedef wtype TWorkingType;                                                                                 \
        static constexpr type mMin=min;                                                                                 \
        static constexpr type mMax=max;                                                                                 \
    }

namespace buola { namespace img {

DECLARE_CHANNEL_TYPE(8,uint8_t,int32_t,0,255);
DECLARE_CHANNEL_TYPE(16,uint16_t,int32_t,0,255);
DECLARE_CHANNEL_TYPE(32,uint32_t,int32_t,0,255);
DECLARE_CHANNEL_TYPE(64,uint64_t,int64_t,0,255);
DECLARE_CHANNEL_TYPE(8s,int8_t,int32_t,-128,127);
DECLARE_CHANNEL_TYPE(16s,int16_t,int32_t,-128,127);
DECLARE_CHANNEL_TYPE(32s,int32_t,int32_t,-128,127);
DECLARE_CHANNEL_TYPE(64s,int64_t,int64_t,-128,127);
DECLARE_CHANNEL_TYPE(32f,float,float,0.0f,1.0f);
DECLARE_CHANNEL_TYPE(64f,double,double,0.0,1.0);

template<typename tFrom,typename tTo>
struct CChannelConversion
{
    typedef typename tTo::TType tToValue;
    typedef typename tFrom::TType tFromValue;
    
    tToValue operator()(tFromValue pV) const
    {
        return tToValue(pV);
    }
};
    
template <>
struct CChannelConversion<TChannel_8,TChannel_32f>
{
    float operator()(uint8_t pV) const
    {
        return pV/255.0f;
    }
};

template <>
struct CChannelConversion<TChannel_32f,TChannel_8>
{
    uint8_t operator()(float pV) const
    {
        return uint8_t(pV*255.0f);
    }
};

/*namespace img*/ } /*namespace buola*/ }

#endif
